/**
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.tasktodorid;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Dialog;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.tasktodorid.util.ConstUtil;
import com.tasktodorid.util.TodoUtil;

/**
 * @author qu sheng
 *
 */
public class TasksList extends ListActivity {

    public final static int MODE_ALL = 0;
    public final static int MODE_UNCOMPLETE = 1;
    public final static int MODE_CATALOGS = 2;


    private final static String TAG = TasksList.class.getName();
    private int modeList;

    private static String[] COLUMNS = {"name"};

    ////////////////////////-------------- protected

    /* (non-Javadoc)
     * @see android.app.ListActivity#onListItemClick(android.widget.ListView, android.view.View, int, long)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub[onCreate]
        Log.d(TAG, "onCreate Begin");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tasks);

        this.modeList = getIntent().getExtras().getInt(ConstUtil.TASKS_MODE_LIST_KEY);
        ArrayList listTasks = new ArrayList();
        switch(this.modeList) {
        case MODE_ALL:
            listTasks = readAllTasks();
            break;
        case MODE_UNCOMPLETE:
            listTasks = readUnComplete();
            break;
        case MODE_CATALOGS:
            String cid = getIntent().getExtras().getString("cid");
            listTasks = readTasksByCatalog(cid);
            break;
        }
        SimpleAdapter adapter = new SimpleAdapter(this, listTasks,
                R.layout.listview_tasks, COLUMNS, new int[] {R.id.txtTaskName});
        setListAdapter(adapter);

        Log.d(TAG, "onCreate End");
    }

    /* (non-Javadoc)
     * @see android.app.ListActivity#onListItemClick(android.widget.ListView, android.view.View, int, long)
     */
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub[onListItemClick]
        super.onListItemClick(l, v, position, id);
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onResume()
     */
    @Override
    protected void onResume() {
        // TODO Auto-generated  method stub [onResume]
        super.onResume();
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0,Menu.FIRST, 0, R.string.menu_add);

        return true;
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()) {
        case Menu.FIRST:
            break;
        }

        return super.onOptionsItemSelected(item);
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onCreateDialog(int)
     */
    @Override
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub [onCreateDialog]
        return super.onCreateDialog(id);
    }


    ////////////////////////-------------- private

    private ArrayList readUnComplete() {
        StringBuilder sb = new StringBuilder();

        sb.append("select task.tid tid, task.name name ");
        sb.append("from m_task task left join sub_task sub ");
        sb.append("on (sub.sub_tid = task.tid ");
        sb.append("and sub.super_tid = '') ");
        sb.append("where task.completerate < 100");

        return getTasksBySql(sb.toString());
    }

    private ArrayList readAllTasks() {
        StringBuilder sb = new StringBuilder();

        sb.append("select task.tid tid, task.name name ");
        sb.append("from m_task task left join sub_task sub ");
        sb.append("on (sub.sub_tid = task.tid ");
        sb.append("and sub.super_tid = '') ");

        return getTasksBySql(sb.toString());
    }

    private ArrayList readTasksByCatalog(String cid) {
        StringBuilder sb = new StringBuilder();

        sb.append("select task.tid tid, task.name name ");
        sb.append("from catalog_task ct left join m_task task ");
        sb.append("on ( ct.tid = task.tid) where ct.cid ='");
        sb.append(cid);
        sb.append("' ");

        return getTasksBySql(sb.toString());
    }

    /**
     * @param sql
     * @return
     */
    private ArrayList getTasksBySql(String sql) {
        Cursor cursor = TodoUtil.dbAccess.rawQuery(sql, null);
        ArrayList listTasks = new ArrayList();
        while (cursor.moveToNext()) {
            HashMap data = new HashMap();
            data.put("tid", cursor.getString(0));
            data.put(COLUMNS[0], cursor.getString(1));
            listTasks.add(data);
        }
        return listTasks;
    }
}
